﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Bamboo.Utils
{
    public class TripleDESCipher
    {
        public string Key { get; }
        public string Prefix { get; }

        /// <summary>
        /// 实例化一个3DES密码机
        /// </summary>
        /// <param name="key">密钥</param>
        /// <param name="prefix">密文前缀</param>
        public TripleDESCipher(string key, string prefix= "ENCRYPT:")
        {
            this.Key = key;
            this.Prefix = prefix;
        }
        /// <summary>
        /// 3DES解密
        /// </summary>
        /// <param name="input">密文</param>
        /// <returns></returns>
        public string Decrypt(string input)
        {
            if (input == null) return "";
            if (!IsEncrypted(input)) return input;
           
            string text = input.Substring(Prefix.Length);

            TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
            MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();
            DES.Key = hashMD5.ComputeHash(Encoding.Default.GetBytes(Key));
            DES.Mode = CipherMode.ECB;
            ICryptoTransform DESDecrypt = DES.CreateDecryptor();
            string result = "";
            try
            {
                byte[] Buffer = Convert.FromBase64String(text);
                result = Encoding.Default.GetString(DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
            }
            catch (System.Exception e)
            {
                throw new Exception("3DES解密失败：" + input, e);
            }
            return result;
        }

        /// <summary>
        /// 判断输入文本是否已加密，根据前缀
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public bool IsEncrypted(string input)
        {
            if (string.IsNullOrWhiteSpace(input))return false;
            return input.StartsWith(Prefix);
        }

        /// <summary>
        /// 3DES加密
        /// </summary>
        /// <param name="input">明文</param>
        /// <returns></returns>
        public string Encrypt(string input)
        {
            if (input == null) return "";
            if (IsEncrypted(input)) return input;
            TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
            MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();
            DES.Key = hashMD5.ComputeHash(Encoding.Default.GetBytes(Key));
            DES.Mode = CipherMode.ECB;

            ICryptoTransform DESEncrypt = DES.CreateEncryptor();

            byte[] Buffer = Encoding.Default.GetBytes(input);
            string ret = Prefix + Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
            return ret;
        }
    }
}
